#include <vector>

using namespace std;

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

/*
inorder:
[左子树][根][右子树]
postorder:
[左子树][右子树][根]
*/

class Solution {
public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        if (inorder.empty() || postorder.empty()) {
            return nullptr;
        }

        // 先找到根节点
        TreeNode* root = new TreeNode(postorder[postorder.size() - 1]);

        // 从inorder中找到左子树的长度
        for (int i = 0; i < inorder.size(); i++) {
            if (inorder[i] != root->val) {
                continue;
            }

            vector<int> left_inorder(inorder.begin(), inorder.begin() + i);
            vector<int> left_postorder(postorder.begin(), postorder.begin() + i);
            vector<int> right_inorder(inorder.begin() + i + 1, inorder.end());
            vector<int> right_postorder(postorder.begin() + i, postorder.end() - 1);

            root->left = buildTree(left_inorder, left_postorder);
            root->right = buildTree(right_inorder, right_postorder);

            break;
        }

        return root;
    }
};

int main() {
    return 0;
}